from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from booktest.models import BookInfo
from booktest.serializers import BookInfoSerializer


#自定义分页类
class StandardResultPagination(PageNumberPagination):
    # 分页默认页容量
    page_size = 2
    # 分页时最大页容量
    max_page_size = 5
    # 获取分页数据的时候，传递页容量的参数名称
    page_size_query_param = 'page_size'

# 写一个类视图，提供1个接口
# 1.获取所有的图书的信息 GET /books/
class BookListView(ListAPIView):
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer
    # 指定过滤字段
    filter_fields = ('btitle','bread')

    # 排序
    filter_backends = [OrderingFilter]

    # 字典字段排序
    ordering_field = ('id','bread','bpub_date')

    # 关闭分页
    # pagination_class = None

    pagination_class = StandardResultPagination